<?php

class Sistema_Form_Gradecor extends Zend_Form
{

    public function init()
    {
        $front = Zend_Controller_Front::getInstance();
        $controller = $front->getRequest()->getControllerName();
        $module = $front->getRequest()->getModuleName();
        $url = $front->getBaseUrl().'/'.$module.'/'.$controller;
        
        $this->setName('form_'.$controller);
        $this->setAttrib('class', 'formCtrl');
        
        $id = $front->getRequest()->getParam('id',0);
        //$mat_id = $front->getRequest()->getParam('mat_id',0);
        
        //$url = $front->getBaseUrl().'/'.$module.'/'.$controller.'/index/mat_id/'.$mat_id;
        if($id > 0){
            $data = new Sistema_Model_DbTable_Gradecor();
            $data = $data->fetchRow('GRC_ID ='.$id);
        }
        
        $el = new Zend_Form_Element_Hidden('GRC_ID',array('disableLoadDefaultDecorators' =>true));
        $el->addDecorators(array(array('ViewHelper'),array('Errors')))
            ->addFilter('Int');
        $this->addElement($el);
        
        $el = new Zend_Form_Element_Text('GRC_NOM');
        $el->setLabel('Nome')
        ->setRequired(true)
        ->addFilters(array('StripTags','StringTrim'))
        ->addValidator('NotEmpty');
        $el->setAttrib('required', '');
        $this->addElement($el);
        
        $obj = new Sistema_Model_DbTable_Fornecedor();
        $row = $obj->fetchAll()->toArray();
        $arr = array(''=>'Nenhum');
        foreach($row as $r) $arr[$r['FOR_ID']] = $r['FOR_NOM']; 
        
        $el = new Zend_Form_Element_Select('FOR_ID',array('onchange' => ''));
        $el->setLabel('Fornecedor')->setRequired(true)->addValidator('NotEmpty')->addMultiOptions($arr);
        $this->addElement($el);
        
        $arr = array(''=>'Nenhum');   
        if($id >0){
            $obj = new Sistema_Model_DbTable_Material();
            $row = $obj->fetchAll('FOR_ID ='.$data['FOR_ID']);
            foreach($row as $r) $arr[$r['MAT_ID']] = $r['MAT_NOM'];
        }
        $el = new Zend_Form_Element_Select('MAT_ID',array('onchange' => ''));
        $el->setLabel('Material')->setRegisterInArrayValidator(false)->addValidator('NotEmpty')->addMultiOptions($arr);
        $this->addElement($el);
        
        /*
        $el = new Zend_Form_Element_Text('GRC_COR');
        $el->setLabel('COR')
        //->setRequired(true)
        ->addFilters(array('StripTags','StringTrim'))
        ->addValidator('NotEmpty');
        //$el->setAttrib('required', '');
        $this->addElement($el); */
        
        $html = '';
        $htmli = '';
        $arr = array();
        $arr_set = array();
        
        $htmli = '<select id="COR_ID[]" name="COR_ID[]" class="chosen-select" multiple="multiple" style="width:500px;">';
        
        if($id >0){
            
            $obj = new Sistema_Model_DbTable_Cor();
            $row = $obj->fetchAll('MAT_ID ='.$data['MAT_ID']);
            
            $cor = new Sistema_Model_DbTable_GradecorHasCor();
            $cor = $cor->fetchAll('GRC_ID = '.$id.' and GHC_CORPRI = 1');
            foreach($cor as $c){
                $arr_set[$c['COR_ID']] = $c['COR_ID'];
            }
            
            foreach($row as $r){
                $selected = array_search($r['COR_ID'], $arr_set) > '' ? 'selected=""':'';            
                $htmli .= '<option '.$selected.' value="'.$r['COR_ID'].'">'.$r['COR_NOM'].'</option>'; 
            }
        }
        
        $htmli .= '</select><br><br>';
        $html .= '<div id="cmb_cores">'.$htmli.'</div>';
        
        $el = new Zend_Form_Element('COR_IDS',array( 'helper'=>'formNote', 'value'=> $html ));
        $el->setLabel('Cores');
        $this->addElement($el);
        
        $html = '';
        $htmli = '';
        $arr = array();
        $arr_set = array();
        $where = 'CTS_ID > 0';
        
        $htmli = '<select id="CTS_ID[]" name="CTS_ID[]" class="chosen-select" multiple="multiple" style="width:500px;">';
        
        if($id >0){
            $arr = array();
            $list_seg = '';
            $cor = new Sistema_Model_DbTable_MaterialHasCat();
            $cor = $cor->fetchAll('MAT_ID = '.$data['MAT_ID']);
            foreach($cor as $c){

                $cat = new Sistema_Model_DbTable_Categoria();
                $cat = $cat->fetchRow('CAT_ID = '.$c['CAT_ID']);
                $arr[$cat['CTS_ID']] = $cat['CTS_ID'];
            }

            if(is_array($arr)) $list_seg = implode(',', $arr);

            if($list_seg > '') $where .= ' and CTS_ID in('.$list_seg.')';

            $obj = new Sistema_Model_DbTable_Catsegmento;
            $row = $obj->fetchAll($where);
        }
        else{
            $obj = new Sistema_Model_DbTable_Catsegmento;
            $row = $obj->fetchAll();
        }
 
        
        if($id > 0){
           foreach(explode(',',$data['CTS_ID']) as $k => $it) $arr_set[$it] = $it;
        }
        
        foreach($row as $r){
            $selected = array_search($r['CTS_ID'], $arr_set) > '' ? 'selected=""':'';            
            $htmli .= '<option '.$selected.' value="'.$r['CTS_ID'].'">'.$r['CTS_NOM'].'</option>'; 

        }
        
        $htmli .= '</select><br>';
        $html = '<div id="cmb_segmentos">'.$htmli.'</div>';
        
        $el = new Zend_Form_Element('CTS_IDS',array( 'helper'=>'formNote', 'value'=> $html ));
        $el->setLabel('Segmentos');
        $this->addElement($el);
        
        $el = new Zend_Form_Element_Checkbox('GRC_STA',array('checked'=>'checked'));
        $el->setLabel('Status');
        $this->addElement($el);
        
        $btn = new Zend_Form_Element_Submit('submit',array('disableLoadDefaultDecorators' =>true));
        $btn->setLabel('Gravar');
        $btn->addDecorators(array(array('ViewHelper'),array('Errors')));
        $btn->setAttrib('id', 'submitbutton');
        $btn->setAttrib('class', 'btn-button btn-ok');
        $this->addElement($btn);
        
        $btn = new Zend_Form_Element_Button('Cancelar',array('disableLoadDefaultDecorators' =>true));
        //$btn->addDecorators(array(array('ViewHelper'),array('Errors')));
        $btn->setDecorators(array(array('Description', array('escape' => false, 'tag' => false))));
        $btn->setDescription('<a href="'.$url.'" class="btn-button btn-cancel">Cancelar</a>');
        $btn->setAttrib('id', 'btncancel');
        $btn->setAttrib('class', 'btn-button btn-cancel');
        $this->addElement($btn);
        
    }


}

